package com.bicon.botu.mall.coupon.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.core.base.common.utils.JsonUtil;
import com.bicon.botu.core.base.repository.redis.RedisHandler;
import com.bicon.botu.mall.coupon.common.CouponStatus;
import com.bicon.botu.mall.coupon.dto.CouponsPageDto;
import com.bicon.botu.mall.coupon.entity.Coupons;
import com.bicon.botu.mall.coupon.service.ICouponsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 优惠券实体类 管理端对接接口
 * </p>
 *
 * @author 吕祥
 * @since 2017-07-19
 */
@RestController
@RequestMapping("/V1.0.0/coupon")
public class CouponsController extends BaseController {

    @Autowired
    private ICouponsService iCouponsService;

    @Autowired
    private RedisHandler redisHandler;

    /**
     * 新增 优惠券
     *
     * @param name           活动名称
     * @param discountAmount 优惠金额
     * @param orderAmount    订单最小金额
     * @param number         优惠券数量
     * @param firsDate       开始使用日期
     * @param lastDate       最后使用日期
     * @param issueDate      优惠券发行日期
     * @param endingDate     优惠券发行结束日期
     * @param path           优惠券使用范围
     * @param remarks        备注
     * @param shopName       商家名称
     * @param type           优惠券类型： 0 = 全场， 1 = 品牌， 2 = 商品， 3 = 分类
     * @param issueState     发行状态:  0 = 发行， 1 = 停止发行
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public BaseResult add(@RequestParam String name, @RequestParam String discountAmount, @RequestParam String orderAmount,
                          @RequestParam Integer number, @RequestParam String firsDate, @RequestParam String lastDate,
                          @RequestParam String issueDate, @RequestParam String endingDate, @RequestParam String path,
                          @RequestParam(required = false) String remarks, @RequestParam(required = false) String shopName,
                          @RequestParam Integer type, @RequestParam int issueState) {
        if (StringUtils.isAnyBlank(name, discountAmount, orderAmount, String.valueOf(number), firsDate, lastDate, issueDate, endingDate, path,
                String.valueOf(type), String.valueOf(issueState))) {
            return sendResult(CouponStatus.RESPONSE_CODE_400, "某个参数有空格或null");
        }
        int ten = 10;
        if (name.length() > ten) {
            return new BaseResult(CouponStatus.RESPONSE_CODE_401, CouponStatus.RESPONSE_MESSAGE_401);
        }
        int five = 5;
        if (type > five) {
            return new BaseResult(CouponStatus.RESPONSE_CODE_401, CouponStatus.RESPONSE_MESSAGE_401);
        }

        return iCouponsService.add(name, discountAmount, orderAmount, number, firsDate,
                lastDate, issueDate, endingDate, path, remarks, shopName, type, issueState);
    }

    /**
     * 修改优惠券
     *
     * @param id             优惠券id
     * @param name           活动名称
     * @param discountAmount 优惠金额
     * @param orderAmount    订单最小金额
     * @param number         优惠券数量
     * @param firsDate       开始使用日期
     * @param lastDate       最后使用日期
     * @param issueDate      优惠券发行日期
     * @param endingDate     优惠券发行结束日期
     * @param path           优惠券使用范围
     * @param remarks        备注
     * @param shopName       商家名称
     * @param type           优惠券类型： 0 = 全场， 1 = 品牌， 2 = 商品， 3 = 分类
     * @return
     */
    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public BaseResult updateById(@RequestParam String id, @RequestParam(required = false) String name, @RequestParam(required = false) String discountAmount,
                                 @RequestParam(required = false) String orderAmount, @RequestParam(required = false) Integer number,
                                 @RequestParam(required = false) String firsDate, @RequestParam(required = false) String lastDate,
                                 @RequestParam(required = false) String issueDate, @RequestParam(required = false) String endingDate,
                                 @RequestParam(required = false) String path, @RequestParam(required = false) String remarks,
                                 @RequestParam(required = false) String shopName, @RequestParam(required = false) Integer type) {
        if (StringUtils.isNotBlank(name)) {
            int ten = 10;
            if (name.length() > ten) {
                return new BaseResult(CouponStatus.RESPONSE_CODE_401, CouponStatus.RESPONSE_MESSAGE_401);
            }
        }
        int five = 5;
        if (type > five) {
            return new BaseResult(CouponStatus.RESPONSE_CODE_401, CouponStatus.RESPONSE_MESSAGE_401);
        }
        return iCouponsService.updateById(id, name, discountAmount, orderAmount, number, firsDate, lastDate, issueDate, endingDate, path, remarks, shopName, type);
    }

    /**
     * 批量删除优惠券
     *
     * @param idJson 优惠券id集合(Json格式参数)
     * @return
     */
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    public BaseResult deleteById(@RequestParam String idJson) {
        if (StringUtils.isAnyBlank(idJson)) {
            return sendResult400();
        }
        return iCouponsService.deleteById(idJson);
    }

    /**
     * 发行
     *
     * @param id         优惠券id
     * @param issueState 发行状态:  0 = 发行， 1 = 停止发行
     * @return
     */
    @RequestMapping(value = "/issue", method = RequestMethod.POST)
    public BaseResult issue(@RequestParam String id, @RequestParam int issueState) {
        if (StringUtils.isAnyBlank(id, String.valueOf(issueState))) {
            return sendResult400();
        }
        return iCouponsService.issue(id, issueState);
    }

    /**
     * 优惠券分页查询
     *
     * @param pageNo   页面数
     * @param pageSize 每页大小
     * @param name     活动名称
     * @return
     */
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public BaseResult page(@RequestParam int pageNo, @RequestParam int pageSize, @RequestParam(required = false) String name) {
        if (StringUtils.isAnyBlank(String.valueOf(pageNo), String.valueOf(pageSize))) {
            return sendResult400();
        }
        Page<CouponsPageDto> page = iCouponsService.page(pageNo, pageSize, name);
        if (null != page) {
            return new BaseResult(CouponStatus.RESPONSE_CODE_200, CouponStatus.RESPONSE_MESSAGE_200, page);
        } else {
            return new BaseResult(CouponStatus.RESPONSE_CODE_602, CouponStatus.RESPONSE_MESSAGE_602);
        }
    }

    /**
     * 查看优惠券详情
     *
     * @param id 优惠券id
     * @return
     */
    @RequestMapping(value = "/findById", method = RequestMethod.POST)
    public BaseResult findById(@RequestParam String id) {
        if (StringUtils.isAnyBlank(id)) {
            return sendResult400();
        }
        Coupons coupons = new Coupons().selectOne(new EntityWrapper<Coupons>().eq("id", id).eq("logic_delete", CouponStatus.DEL_LOGIC_NORMAL));
        if (null != coupons) {
            return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, coupons);
        } else {
            return new BaseResult(Constants.RESPONSE_CODE_602, Constants.RESPONSE_MESSAGE_602);
        }
    }

    /**
     * 初始化缓存
     *
     * @return
     */
    @RequestMapping(value = "/initRedis", method = RequestMethod.POST)
    public BaseResult initRedis() {
        //redis Key
        String redisKey = CouponStatus.BOTU + CouponStatus.REDIS_KEY_DELIMITER + CouponStatus.MALL + CouponStatus.REDIS_KEY_DELIMITER + CouponStatus.COUPON + CouponStatus.REDIS_KEY_DELIMITER + CouponStatus.COUPONS;
        redisHandler.getLocalRedisTemplate().remove(redisKey, "");
        List<Coupons> couponsList = new Coupons().selectList(new EntityWrapper<Coupons>().eq("logic_delete", CouponStatus.DEL_LOGIC_NORMAL));
        for (Coupons coupons : couponsList) {
            boolean b = redisHandler.getLocalRedisTemplate().cacheHash(redisKey, "", coupons.getId(), JsonUtil.toJson(coupons));
            if (!b) {
                return new BaseResult(CouponStatus.RESPONSE_CODE_600, CouponStatus.RESPONSE_MESSAGE_600);
            }
        }
        return new BaseResult(CouponStatus.RESPONSE_CODE_200, CouponStatus.RESPONSE_MESSAGE_200);
    }
}
